﻿using System.Web.Mvc;
using Illinois.Core.Fakes;
using Illinois.Core.Interfaces;
using Illinois.Core.Services;

namespace Illinois.UI.Controllers
{
  public class LogInController : BaseController
  {
    private readonly ILogInService _logInService;

    // todo: use CASTLE asap
    public LogInController()
      : this(new LogInService(new UserService(new FakeUserRepository())))
    {
    }

    public LogInController(ILogInService logInService)
    {
      _logInService = logInService;
    }

    public ActionResult Index()
    {
      return View();
    }

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Index(string username, string password)
    {
      var user = _logInService.LogIn(username, password);
      if (HttpContext.Session != null) HttpContext.Session[UserSessionName] = user;
      if (user != null)
        return RedirectToAction("Index", "Home");
      return RedirectToAction("Index", "LogIn");
    }
  }
}
